Exporting Trajectories¶

Binder IPYNB HTML

Trajectories and TrajectoryCollections can be converted back to GeoDataFrames that can then be exported to different GIS file formats.

In [1]:
import pandas as pd
import geopandas as gpd
import movingpandas as mpd
import shapely as shp
import hvplot.pandas 

from geopandas import GeoDataFrame, read_file
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta
from holoviews import opts

import warnings
warnings.filterwarnings('ignore')

opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400))

mpd.show_versions()
MovingPandas 0.15.rc1

SYSTEM INFO
-----------
python     : 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:39:05) [MSC v.1929 64 bit (AMD64)]
executable : H:\miniconda3\envs\mpd-ex\python.exe
machine    : Windows-10-10.0.19045-SP0

GEOS, GDAL, PROJ INFO
---------------------
GEOS       : None
GEOS lib   : None
GDAL       : 3.5.0
GDAL data dir: None
PROJ       : 9.0.0
PROJ data dir: H:\miniconda3\pkgs\proj-9.0.0-h1cfcee9_1\Library\share\proj

PYTHON DEPENDENCIES
-------------------
geopandas  : 0.12.2
pandas     : 1.5.3
fiona      : 1.8.21
numpy      : 1.24.1
shapely    : 1.8.2
rtree      : 1.0.0
pyproj     : 3.3.1
matplotlib : 3.6.3
mapclassify: None
geopy      : 2.3.0
holoviews  : 1.14.9
hvplot     : 0.8.2
geoviews   : 1.9.6
stonesoup  : 0.1b11
In [2]:
gdf = read_file('../data/geolife_small.gpkg')
traj_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')
traj_collection
Out[2]:
TrajectoryCollection with 5 trajectories

Converting TrajectoryCollections back to GeoDataFrames¶

Convert to a point GeoDataFrame¶

In [3]:
traj_collection.to_point_gdf()
Out[3]:
id sequence trajectory_id tracker geometry
t
2008-12-11 04:42:14 1 1 1 19 POINT (116.39131 39.89857)
2008-12-11 04:42:16 2 2 1 19 POINT (116.39132 39.89862)
2008-12-11 04:43:26 3 3 1 19 POINT (116.39093 39.89861)
2008-12-11 04:43:32 4 4 1 19 POINT (116.39083 39.89863)
2008-12-11 04:43:47 5 5 1 19 POINT (116.38941 39.89872)
... ... ... ... ... ...
2009-02-25 14:31:04 6993 867 5 2 POINT (116.33719 39.92623)
2009-02-25 14:31:09 6994 868 5 2 POINT (116.33721 39.92624)
2009-02-25 14:31:14 6995 869 5 2 POINT (116.33726 39.92621)
2009-02-25 14:31:19 6996 870 5 2 POINT (116.33729 39.92620)
2009-02-25 14:31:24 6997 871 5 2 POINT (116.33733 39.92619)

5908 rows × 5 columns

Convert to a line GeoDataFrame¶

In [4]:
traj_collection.to_line_gdf()
Out[4]:
id sequence trajectory_id tracker t prev_t geometry
0 2 2 1 19 2008-12-11 04:42:16 2008-12-11 04:42:14 LINESTRING (116.39131 39.89857, 116.39132 39.8...
1 3 3 1 19 2008-12-11 04:43:26 2008-12-11 04:42:16 LINESTRING (116.39132 39.89862, 116.39093 39.8...
2 4 4 1 19 2008-12-11 04:43:32 2008-12-11 04:43:26 LINESTRING (116.39093 39.89861, 116.39083 39.8...
3 5 5 1 19 2008-12-11 04:43:47 2008-12-11 04:43:32 LINESTRING (116.39083 39.89863, 116.38941 39.8...
4 6 6 1 19 2008-12-11 04:43:50 2008-12-11 04:43:47 LINESTRING (116.38941 39.89872, 116.39052 39.8...
... ... ... ... ... ... ... ...
5898 6993 867 5 2 2009-02-25 14:31:04 2009-02-25 14:31:02 LINESTRING (116.33715 39.92631, 116.33719 39.9...
5899 6994 868 5 2 2009-02-25 14:31:09 2009-02-25 14:31:04 LINESTRING (116.33719 39.92623, 116.33721 39.9...
5900 6995 869 5 2 2009-02-25 14:31:14 2009-02-25 14:31:09 LINESTRING (116.33721 39.92624, 116.33726 39.9...
5901 6996 870 5 2 2009-02-25 14:31:19 2009-02-25 14:31:14 LINESTRING (116.33726 39.92621, 116.33729 39.9...
5902 6997 871 5 2 2009-02-25 14:31:24 2009-02-25 14:31:19 LINESTRING (116.33729 39.92620, 116.33733 39.9...

5903 rows × 7 columns

Convert to a trajectory GeoDataFrame¶

In [5]:
traj_collection.to_traj_gdf(wkt=True)
Out[5]:
traj_id start_t end_t geometry length direction wkt
0 1 2008-12-11 04:42:14 2008-12-11 05:15:46 LINESTRING (116.39131 39.89857, 116.39132 39.8... 6207.020261 186.681376 LINESTRING M (116.391305 39.898573 1228970534....
1 2 2009-06-29 07:02:25 2009-06-29 11:13:12 LINESTRING (116.59096 40.07196, 116.59091 40.0... 38764.575483 250.585295 LINESTRING M (116.590957 40.071961 1246258945....
2 3 2009-02-04 04:32:53 2009-02-04 11:20:12 LINESTRING (116.38569 39.89977, 116.38565 39.8... 12745.157506 304.115160 LINESTRING M (116.385689 39.899773 1233721973....
3 4 2009-03-10 10:36:45 2009-03-10 12:01:07 LINESTRING (116.38805 39.90342, 116.38804 39.9... 14363.780551 300.732843 LINESTRING M (116.388053 39.903418 1236681405....
4 5 2009-02-25 09:47:03 2009-02-25 14:31:24 LINESTRING (116.38526 39.90027, 116.38525 39.9... 39259.779560 305.200501 LINESTRING M (116.385256 39.90027 1235555223.0...
In [6]:
traj_collection.to_traj_gdf(agg={'tracker':'mode', 'sequence':['min', 'max']})
Out[6]:
traj_id start_t end_t geometry length direction tracker_mode sequence_min sequence_max
0 1 2008-12-11 04:42:14 2008-12-11 05:15:46 LINESTRING (116.39131 39.89857, 116.39132 39.8... 6207.020261 186.681376 19 1 466
1 2 2009-06-29 07:02:25 2009-06-29 11:13:12 LINESTRING (116.59096 40.07196, 116.59091 40.0... 38764.575483 250.585295 0 1090 1986
2 3 2009-02-04 04:32:53 2009-02-04 11:20:12 LINESTRING (116.38569 39.89977, 116.38565 39.8... 12745.157506 304.115160 2 1 1810
3 4 2009-03-10 10:36:45 2009-03-10 12:01:07 LINESTRING (116.38805 39.90342, 116.38804 39.9... 14363.780551 300.732843 2 1 1864
4 5 2009-02-25 09:47:03 2009-02-25 14:31:24 LINESTRING (116.38526 39.90027, 116.38525 39.9... 39259.779560 305.200501 2 1 871

Exporting to GIS file formats¶

These GeoDataFrames can be exported to different file formats using GeoPandas, as documented in https://geopandas.org/docs/user_guide/io.html

In [7]:
export_gdf = traj_collection.to_traj_gdf(agg={'sequence':['min', 'max']})
export_gdf.to_file("temp.gpkg", layer='trajectories', driver="GPKG")
In [8]:
read_file('temp.gpkg').plot()
Out[8]:
<AxesSubplot: >
In [9]:
read_file('temp.gpkg')
Out[9]:
traj_id start_t end_t length direction sequence_min sequence_max geometry
0 1 2008-12-11 04:42:14 2008-12-11 05:15:46 6207.020261 186.681376 1 466 LINESTRING (116.39131 39.89857, 116.39132 39.8...
1 2 2009-06-29 07:02:25 2009-06-29 11:13:12 38764.575483 250.585295 1090 1986 LINESTRING (116.59096 40.07196, 116.59091 40.0...
2 3 2009-02-04 04:32:53 2009-02-04 11:20:12 12745.157506 304.115160 1 1810 LINESTRING (116.38569 39.89977, 116.38565 39.8...
3 4 2009-03-10 10:36:45 2009-03-10 12:01:07 14363.780551 300.732843 1 1864 LINESTRING (116.38805 39.90342, 116.38804 39.9...
4 5 2009-02-25 09:47:03 2009-02-25 14:31:24 39259.779560 305.200501 1 871 LINESTRING (116.38526 39.90027, 116.38525 39.9...
In [ ]: